<template>
  <div>
    <el-table :data="momentList">
      <el-table-column label="序号" type="index" width="50"></el-table-column>
      <el-table-column label="内容" prop="content" show-overflow-tooltip></el-table-column>
      <el-table-column label="发布状态" width="80">
        <template v-slot="scope">
          <el-switch
            v-model="scope.row.published"
            @change="toggleMomentPublish(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="点赞数" prop="likes" width="80"></el-table-column>
      <el-table-column label="创建时间" width="170">
        <template v-slot="scope">{{ scope.row.createTime | dateFormat }}</template>
      </el-table-column>
      <el-table-column label="操作" width="200">
        <template v-slot="scope">
          <el-button
            icon="el-icon-edit"
            size="mini"
            type="primary"
            @click="goEditMomentPage(scope.row.id)"
          >
            编辑
          </el-button>
          <el-popconfirm
            icon="el-icon-delete"
            iconColor="red"
            title="确定删除吗？"
            @onConfirm="deleteMomentById(scope.row.id)"
          >
            <el-button slot="reference" icon="el-icon-delete" size="mini" type="danger">
              删除
            </el-button>
          </el-popconfirm>
        </template>
      </el-table-column>

      <!--分页-->
      <el-pagination
        :current-page="queryBody.pageNum"
        :page-size="queryBody.pageSize"
        :page-sizes="[10, 20, 30, 50]"
        :total="total"
        background
        hide-on-single-page
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handlePageSizeChange"
        @current-change="handleCurrentNumChange"
      ></el-pagination>
    </el-table>
  </div>
</template>

<script>
  import { deleteMomentById, getMomentListByQuery, updatePublished } from '@/api/moment'

  export default {
    name: 'MomentList',
    components: {},
    data() {
      return {
        queryBody: {
          pageNum: 1,
          pageSize: 10,
        },
        momentList: [],
        total: 0,
      }
    },
    computed: {},
    created() {
      this.getMomentList()
    },
    methods: {
      getMomentList() {
        getMomentListByQuery(this.queryInfo).then((res) => {
          this.momentList = res.data.list
          this.total = res.data.total
        })
      },
      //监听 pageSize 改变事件
      handlePageSizeChange(newSize) {
        this.queryBody.pageSize = newSize
        this.getMomentList()
      },
      //监听页码改变的事件
      handleCurrentNumChange(newCurrentNum) {
        this.queryBody.pageNum = newCurrentNum
        this.getMomentList()
      },
      toggleMomentPublish(row) {
        updatePublished(row.id, row.published).then((res) => {
          this.msgSuccess(res.msg)
        })
      },
      // 编辑状态
      goEditMomentPage(id) {
        this.$router.push(`/blog/moment/edit/${id}`)
      },
      deleteMomentById(id) {
        deleteMomentById(id).then((res) => {
          this.msgSuccess(res.msg)
          this.getMomentList()
        })
      },
    },
  }
</script>

<style lang="scss" scoped>
  .el-button + span {
    margin-left: 10px;
  }
</style>
